Added spec for the FormConfigurableAgentPresenter

Dominik Sander 9 years ago
parent
commit
4eabcca235

+ 1 - 0
Gemfile

@@ -99,6 +99,7 @@ group :development, :test do
99 99
   gem 'rspec', '~> 3.0'
100 100
   gem 'rspec-collection_matchers', '~> 1.0.0'
101 101
   gem 'rspec-rails', '~> 3.0.1'
102
+  gem 'rspec-html-matchers', '~> 0.6.1'
102 103
   gem 'shoulda-matchers'
103 104
   gem 'spring'
104 105
   gem 'spring-commands-rspec'

+ 4 - 0
Gemfile.lock

@@ -310,6 +310,9 @@ GEM
310 310
     rspec-expectations (3.0.4)
311 311
       diff-lcs (>= 1.2.0, < 2.0)
312 312
       rspec-support (~> 3.0.0)
313
+    rspec-html-matchers (0.6.1)
314
+      nokogiri (~> 1)
315
+      rspec (~> 3)
313 316
     rspec-mocks (3.0.4)
314 317
       rspec-support (~> 3.0.0)
315 318
     rspec-rails (3.0.2)
@@ -497,6 +500,7 @@ DEPENDENCIES
497 500
   rr
498 501
   rspec (~> 3.0)
499 502
   rspec-collection_matchers (~> 1.0.0)
503
+  rspec-html-matchers (~> 0.6.1)
500 504
   rspec-rails (~> 3.0.1)
501 505
   rturk (~> 2.12.1)
502 506
   ruby-growl (~> 4.1.0)

+ 1 - 0
app/assets/javascripts/application.js.coffee

@@ -5,6 +5,7 @@
5 5
 #= require select2
6 6
 #= require json2
7 7
 #= require jquery.json-editor
8
+#= require jquery.serializeObject
8 9
 #= require latlon_and_geo
9 10
 #= require spectrum
10 11
 #= require_tree ./components

+ 1 - 14
app/assets/javascripts/components/form_configurable.js.coffee

@@ -1,16 +1,4 @@
1 1
 $ ->
2
-  $.fn.serializeObject = ->
3
-    o = {}
4
-    a = @serializeArray()
5
-    $.each a, ->
6
-      if o[@name] isnt `undefined`
7
-        o[@name] = [o[@name]]  unless o[@name].push
8
-        o[@name].push @value or ""
9
-      else
10
-        o[@name] = @value or ""
11
-      return
12
-    o
13
-
14 2
   getFormData = (elem) ->
15 3
     form_data = $("#edit_agent, #new_agent").serializeObject()
16 4
     attribute = $(elem).data('attribute')
@@ -22,7 +10,7 @@ $ ->
22 10
     returnedResults = {}
23 11
     completableDefaultOptions = (input) ->
24 12
       results: [
25
-        (returnedResults[$(input).data('attribute')] || {text: 'Options', children: [{id: '', text: 'loading ...'}]})
13
+        (returnedResults[$(input).data('attribute')] || {text: 'Options', children: [{id: '', text: 'loading ...'}]}),
26 14
         {
27 15
           text: 'Current',
28 16
           children: [id: $(input).val(), text: $(input).val()]
@@ -68,7 +56,6 @@ $ ->
68 56
         type: 'POST',
69 57
         data: form_data
70 58
         success: (data) ->
71
-          console.log data
72 59
           returnedResults[form_data.attribute] = {text: 'Options', children: $.map(data, (d) -> {id: d.value, text: d.name})}
73 60
           $(e.currentTarget).trigger('change')
74 61
           $(e.currentTarget).select2('open')

+ 40 - 0
spec/presenters/form_configurable_agent_presenter_spec.rb

@@ -0,0 +1,40 @@
1
+require 'spec_helper'
2
+
3
+describe FormConfigurableAgentPresenter do
4
+  class FormConfigurableAgentPresenterAgent < Agent
5
+    include FormConfigurable
6
+
7
+    form_configurable :string, roles: :validatable
8
+    form_configurable :text, type: :text, roles: :completable
9
+    form_configurable :boolean, type: :boolean
10
+    form_configurable :array, type: :array, values: [1, 2, 3]
11
+  end
12
+
13
+  before(:all) do
14
+    @presenter = FormConfigurableAgentPresenter.new(FormConfigurableAgentPresenterAgent.new, ActionController::Base.new.view_context)
15
+  end
16
+
17
+  it "works for the type :string" do
18
+    expect(@presenter.option_field_for(:string)).to(
19
+      have_tag('input', with: {:'data-attribute' => 'string', role: 'validatable', type: 'text', name: 'agent[options][string]'})
20
+    )
21
+  end
22
+
23
+  it "works for the type :text" do
24
+    expect(@presenter.option_field_for(:text)).to(
25
+      have_tag('textarea', with: {:'data-attribute' => 'text', role: 'completable', name: 'agent[options][text]'})
26
+    )
27
+  end
28
+
29
+  it "works for the type :boolean" do
30
+    expect(@presenter.option_field_for(:boolean)).to(
31
+      have_tag('input', with: {:'data-attribute' => 'boolean', role: '', name: 'agent[options][boolean]', type: 'radio'})
32
+    )
33
+  end
34
+
35
+  it "works for the type :boolean" do
36
+    expect(@presenter.option_field_for(:array)).to(
37
+      have_tag('select', with: {:'data-attribute' => 'array', role: '', name: 'agent[options][array]'})
38
+    )
39
+  end
40
+end

+ 40 - 0
vendor/assets/javascripts/jquery.serializeObject.js

@@ -0,0 +1,40 @@
1
+//
2
+// Use internal $.serializeArray to get list of form elements which is
3
+// consistent with $.serialize
4
+//
5
+// From version 2.0.0, $.serializeObject will stop converting [name] values
6
+// to camelCase format. This is *consistent* with other serialize methods:
7
+//
8
+//   - $.serialize
9
+//   - $.serializeArray
10
+//
11
+// If you require camel casing, you can either download version 1.0.4 or map
12
+// them yourself.
13
+//
14
+
15
+(function($){
16
+  $.fn.serializeObject = function () {
17
+    "use strict";
18
+
19
+    var result = {};
20
+    var extend = function (i, element) {
21
+      var node = result[element.name];
22
+
23
+  // If node with same name exists already, need to convert it to an array as it
24
+  // is a multi-value field (i.e., checkboxes)
25
+
26
+      if ('undefined' !== typeof node && node !== null) {
27
+        if ($.isArray(node)) {
28
+          node.push(element.value);
29
+        } else {
30
+          result[element.name] = [node, element.value];
31
+        }
32
+      } else {
33
+        result[element.name] = element.value;
34
+      }
35
+    };
36
+
37
+    $.each(this.serializeArray(), extend);
38
+    return result;
39
+  };
40
+})(jQuery);